﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
using Pixysoft.Framework.Configurations;
using System.Globalization;

namespace Pixysoft.Framework.Logs.Core
{
    class Record : IRecord
    {
        IXmlNode node;

        public Record(IXmlNode node)
        {
            this.node = node;
        }

        public string ElementName
        {
            get
            {
                return node.ElementName;
            }
        }

        public string Text
        {
            get
            {
                return node.Text;
            }
            set { node.Text = value; }
        }

        public string Id
        {
            get
            {
                return node.Properties[StringHelper.default_attribute_id];
            }
            internal set { node.Properties[StringHelper.default_attribute_id] = value; }
        }

        public string RawDate
        {
            get
            {
                return node.Properties[StringHelper.default_attribute_time];
            }
            internal set { node.Properties[StringHelper.default_attribute_time] = value; }
        }

        public DateTime Date
        {
            get
            {
                return DateTime.ParseExact(RawDate, StringHelper.default_log_dateformat, CultureInfo.InvariantCulture);
            }
        }

        public string Serialize()
        {
            return Serialize(false);
        }

        public string Serialize(bool indent)
        {
            return node.Serialize(indent);
        }

        public IRecordPropertyCollection Properties
        {
            get
            {
                return new RecordPropertyCollection(node.Properties);
            }
        }

        public IRecordCollection Records
        {
            get
            {
                return new RecordCollection(node.Nodes);
            }
        }

        public int CompareTo(IRecord other)
        {
            if (this.Date > other.Date)
                return 1;

            else if (this.Date == other.Date)
                return 0;

            else
                return -1;
        }
    }
}
